package com.cyx.dao;

import com.cyx.pojo.Department;
import com.cyx.pojo.Employee;
import org.springframework.stereotype.Repository;

import java.util.*;

@Repository
public class EmployeeDao {
    //private static Collection<Employee> employees = new Hashtable<Employee>();
    private static List<Employee> employees = new ArrayList<>();
    private static List<Department> departments = new ArrayList<>();
    private static Integer employeeId = 1005;
    static {
        departments.add(new Department(1,"培训部"));
        departments.add(new Department(2,"后勤部"));
        departments.add(new Department(3,"研发部"));
        employees.add(new Employee(1001,"詹三","4324324@qq.com","税友大厦",new Date(),1,departments.get(0)));
        employees.add(new Employee(1002,"武汉","4324324@qq.com","税友大厦",new Date(),0,departments.get(1)));
        employees.add(new Employee(1003,"光谷","4324324@qq.com","税友大厦",new Date(),1,departments.get(2)));
        employees.add(new Employee(1004,"涣散","4324324@qq.com","税友大厦",new Date(),0,departments.get(2)));
    }
    public List<Employee> getEmployees(){
        return employees;
    }

    public List<Department> getDepartments(){
        return departments;
    }

    public Boolean saveEmployee(Employee employee){
        employee.setId(employeeId);
        employees.add(employee);
        employeeId ++;
        return true;
    }

    public Employee getEmployee(Integer id){
        Employee employee = null;
        for (Employee emp : employees) {
            if (emp.getId().equals(id)){
                employee = emp;
                break;
            }
        }
        return employee;
    }

    public Boolean updateEmployee(Employee employee){
        int index = -1;
        for (Employee emp : employees) {
            if (emp.getId().equals(employee.getId())){
                index = employees.indexOf(emp);
                break;
            }
        }
        employees.set(index,employee);
        return true;
    }

    public Boolean deleteEmployee(Integer id){
        for (Employee emp : employees) {
            if (emp.getId().equals(id)){
                employees.remove(emp);
                break;
            }
        }
        return true;
    }
}
